package com.conan.mqtt.consumer.mqttpconsumer.callback;


import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.Objects;

public class MqttConsumerCallBack implements MqttCallback {

    /**
     * 客户端断开连接的回调
     */
    @Override
    public void connectionLost(Throwable throwable) {
        throwable.printStackTrace();
        System.out.println("与服务器断开连接，可重连");
    }

    /**
     * 消息到达的回调
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println(String.format("接收消息主题 : %s", topic));
        System.out.println(String.format("接收消息Qos : %d", message.getQos()));
        System.out.println(String.format("接收消息内容 : %s", new String(message.getPayload())));
        System.out.println(String.format("接收消息retained : %b", message.isRetained()));
    }

    /**
     * 消息发布成功的回调
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        try {
            MqttMessage message = iMqttDeliveryToken.getMessage();
            if (Objects.isNull(message)) {
                System.out.println("message is null");
                return;
            }
            System.out.println(String.format("接收消息主题 : %s", iMqttDeliveryToken.getTopics()));
            System.out.println(String.format("接收消息Qos : %d", message.getQos()));
            System.out.println(String.format("接收消息内容 : %s", new String(message.getPayload())));
            System.out.println(String.format("接收消息retained : %b", message.isRetained()));
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }

    }

}
